<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts">
  import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { GaugeChart } from 'echarts/charts';
  import { cloneDeep } from 'lodash-es';
  export default defineComponent({
    name: 'Gauge',
    props: {
      chartData: {
        type: Object as PropType<Object>,
        default: () => [],
      },
      option: {
        type: Object,
        default: () => ({}),
      },
      width: {
        type: String as PropType<string>,
        default: '100%',
      },
      height: {
        type: String as PropType<string>,
        default: 'calc(100vh - 78px)',
      },
    },
    setup(props) {
      const chartRef = ref<HTMLDivElement | null>(null);
      const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
      const option = reactive({
        series: [
          {
            type: 'gauge',
            startAngle: 180,
            endAngle: 0,
            center: ['50%', '65%'],
            radius: '100%',
            min: 0,
            max: 1,
            splitNumber: 8,
            axisLine: {
              lineStyle: {
                width: 6,
                color: [
                  [0.25, '#FF6E76'],
                  [0.5, '#FDDD60'],
                  [0.75, '#58D9F9'],
                  [1, '#7CFFB2']
                ]
              }
            },
            pointer: {
              icon: 'path://M12.8,0.7l12,40.1H0.7L12.8,0.7z',
              length: '12%',
              width: 8,
              offsetCenter: [0, '-50%'],
              itemStyle: {
                color: 'auto'
              }
            },
            axisTick: {
              length: 12,
              lineStyle: {
                color: 'auto',
                width: 1
              }
            },
            splitLine: {
              length: 15,
              lineStyle: {
                color: 'auto',
                width: 2
              }
            },
            axisLabel: {
              color: '#464646',
              fontSize: 12,
              distance: -40,
              rotate: 'tangential',
              formatter: function (value) {
                if (value === 1) {
                  return '100%';
                } else if (value === 0.8) {
                  return '80%';
                } else if (value === 0.5) {
                  return '50%';
                } else if (value === 0.2) {
                  return '20%';
                } else if (value === 0) {
                  return '0%';
                }
                return '';
              }
            },
            title: {
            },
            detail: {
              fontSize: 24,
              offsetCenter: [0, '-20%'],
              valueAnimation: true,
              formatter: function (value) {
                return Math.round(value * 100) + '';
              },
              color: 'inherit'
            },

            data: [
              {
                value: 0.4,
                name: '本地磁盘',
              },
            ],
          },
        ],
      });

      watchEffect(() => {
        props.chartData && initCharts();
      });

      function initCharts() {
        echarts.use(GaugeChart);
        if (props.option) {
          Object.assign(option, cloneDeep(props.option));
        }
        option.series[0].data[0].name = props.chartData.name;
        option.series[0].data[0].value = props.chartData.value;
        setOptions(option);
      }
      return { chartRef };
    },
  });
</script>
